package com.contactbook.web.client;

import com.contactbook.web.client.dto.ContactDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ContactSheetWidget extends View {

	private VerticalPanel content;
	
	private Label contactFullName, contactAddress, contactZipCode, contactTown, contactCountry, contactEmail, contactAge, contactMaritalStatus;
	
	private final static String TITLE = "Contact Sheet";
	
	public ContactSheetWidget(ContactBook contactBook) {
		super(contactBook);
	}
	
	@SuppressWarnings("deprecation")
	@Override
	public VerticalPanel getContent() {
		if (contactBook.isLogged()) {
			clearContent();
			buildView();
			if (contactBook.getCurrentSession().parameters.get(Constants.SessionParameterContactId) != null) {
				int contactId = (Integer)contactBook.getCurrentSession().parameters.get(Constants.SessionParameterContactId);
				contactBook.getCurrentSession().clearParameter(Constants.SessionParameterContactId);
				loadContact(contactId);
			} else {
				return new ErrorPanel();
			}
			return content;
		} else
			contactBook.switchView(Constants.LoginViewName);
		return null;
	}
	
	private VerticalPanel buildView() {
		
		content = new VerticalPanel();
		
		DecoratorPanel panelContactSheet = new DecoratorPanel();
		
		VerticalPanel panelWidgets = new VerticalPanel();
		panelWidgets.setWidth(Constants.SmallPanelWidgetsStandartWidth);
		panelWidgets.setSpacing(5);
		
		HorizontalPanel panelHeaderSheet = new HorizontalPanel();
		panelHeaderSheet.setWidth(Constants.GroupNameHeaderPanelWidth);
		panelHeaderSheet.setStyleName("gwt-label-panelGoupTitleHr");
		panelHeaderSheet.setSpacing(3);
		
		Image contactIcon = new Image(Constants.IconContactPath);
		contactIcon.setSize(Constants.IconSizeConstant, Constants.IconSizeConstant);
		
		contactFullName = new Label("");
		contactFullName.setStyleName("gwt-label-detailedGroupNameHeader");
		
		panelHeaderSheet.add(contactIcon);
		panelHeaderSheet.add(contactFullName);
		
		// information		
		Label labelInfoDetails = new Label("Details");
		labelInfoDetails.setStyleName("gwt-label-labelnote");
		
		FlexTable table = new FlexTable();
		
		contactAddress = new Label("");		
		contactZipCode = new Label("");
		contactTown = new Label("");
		contactCountry = new Label("");
		contactEmail = new Label("");
		contactAge = new Label("");
		contactMaritalStatus = new Label("");
		
		table.setWidget(0, 0, new Label("Address :"));
		table.setWidget(0, 1, contactAddress);
		
		table.setWidget(1, 0, new Label("Zipcode :"));
		table.setWidget(1, 1, contactZipCode);
		
		table.setWidget(2, 0, new Label("Town :"));
		table.setWidget(2, 1, contactTown);
		
		table.setWidget(3, 0, new Label("Country :"));
		table.setWidget(3, 1, contactCountry);
		
		table.setWidth("200px");
		table.setCellSpacing(5);
		table.getCellFormatter().setWidth(0, 0, "90px");
		
		Label labelInfoOtherInformation = new Label("Other information");
		labelInfoOtherInformation.setStyleName("gwt-label-labelnote");
		
		FlexTable table2 = new FlexTable();
		
		table2.setWidget(0, 0, new Label("Age :"));
		table2.setWidget(0, 1, contactAge);
		
		table2.setWidget(1, 0, new Label("Marital status :"));
		table2.setWidget(1, 1, contactMaritalStatus);
		
		panelWidgets.add(panelHeaderSheet);
		panelWidgets.add(labelInfoDetails);
		panelWidgets.add(table);
		panelWidgets.add(labelInfoOtherInformation);
		panelWidgets.add(table2);
		
		HorizontalPanel panelButton = new HorizontalPanel();
		panelButton.setSpacing(6);
		panelButton.setWidth(Constants.SmallPanelWidgetsStandartWidth);
		
		Button backButton = new Button("Back");
		backButton.setStyleName("buttonform");
		backButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent arg0) {
				contactBook.switchView(Constants.ListOfContactsViewName);
			}			
		});
		panelButton.add(backButton);
		
		panelWidgets.add(panelButton);
		
		panelContactSheet.add(panelWidgets);
		
		content.add(panelContactSheet);
		
		return content;
	}

	@Override
	public String getTitle() {
		return TITLE;
	}
	
	private void clearContent() {
		if (content != null)
			content.clear();
	}
	
	private void loadContact(int contactId) {
		AsyncCallback<ContactDTO> callback = new AsyncCallback<ContactDTO>() {

			@Override
			public void onFailure(Throwable arg0) {
			}

			@Override
			public void onSuccess(ContactDTO result) {
				contactFullName.setText(result.getFirstName() + " " + result.getLastName());
				
				if (result.getAddress() != null)
					contactAddress.setText(result.getAddress());
				else
					contactAddress.setText("-");
				
				if (result.getZipCode() != 0)
					contactZipCode.setText(String.valueOf(result.getZipCode()));
				else
					contactZipCode.setText("-");
					
				if (result.getTown() != null)
					contactTown.setText(result.getTown());
				else
					contactTown.setText("-");
				
				if (result.getCountry() != null)
					contactCountry.setText(result.getCountry());
				else
					contactCountry.setText("-");
				
				if (result.getEmail() != null)
					contactEmail.setText(result.getEmail());
				else
					contactEmail.setText("-");
				
				if (result.getAge() != 0)
					contactAge.setText(String.valueOf(result.getAge()));
				else
					contactAge.setText("-");
				
				if (result.getMaritalStatus() != null) {
					switch (result.getMaritalStatus()) {
					case ANNULLED :
						contactMaritalStatus.setText("Annuled");
						break;
					case COHABITATING:
						contactMaritalStatus.setText("Cohabitating");
						break;
					case DIVORCED:
						contactMaritalStatus.setText("Divorced");
						break;
					case ENGAGED:
						contactMaritalStatus.setText("Engaged");
						break;
					case MARRIED:
						contactMaritalStatus.setText("Married");
						break;
					case SEPARATED:
						contactMaritalStatus.setText("Separated");
						break;
					case SINGLE:
						contactMaritalStatus.setText("Single");
						break;
					case WIDOWED:
						contactMaritalStatus.setText("Widowed");
						break;
					default:
						break;
					}
				} else
					contactMaritalStatus.setText("-");
			}			
		};
		contactBook.getContactService().getContactById(contactId, contactBook.getCurrentSession().getLogin(), contactBook.getCurrentSession().getPassword(), callback);
	}
}
